from kafka import KafkaProducer
import json
# 1.创建KafkaProducer连接实例
producer = KafkaProducer(
	  # 本地Kafka服务器的9092默认端口
    bootstrap_servers=['127.0.0.1:9092']
)
# 2. 定义发送的数据，JSON格式
userInfo = {
    'username': '张三',
    'password': '112233',
    'mobile': '13712344321',
    'gender': '男',
    'createTime': '2016-05-30'
}
data = json.dumps(userInfo, ensure_ascii=True).encode("utf-8")
# 3. 定义topic，并向此发送数据
topic = 'newUser'
producer.send(topic, data)
# 4. 关闭连接
producer.close()

